package com.itheima.Service.Impl;

import com.itheima.Mapper.TeacherMapper;
import com.itheima.Mapper.studentMapper;
import com.itheima.Pojo.LoginInfo;
import com.itheima.Pojo.Student;
import com.itheima.Pojo.Teacher;
import com.itheima.Service.LoginServise;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
public class LoginServiseimpl implements LoginServise {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private studentMapper studentMapper;


    @Override
    public LoginInfo tea_login(Teacher teacher) {
        //1. 调用mapper接口, 根据用户名和密码查询员工信息
        Teacher t = teacherMapper.selectByUsernameAndPassword(teacher);

        //2. 判断: 判断是否存在这个教师, 如果存在, 组装登录成功信息
        if(t != null){
            log.info("登录成功, 教师信息: {}", t);
            //生成JWT令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("teacherid", t.getTeacherId());
            claims.put("username", t.getName());
            String jwt = JwtUtils.generateToken(claims);


            String role = "教师";

            return new LoginInfo(t.getTeacherId(), t.getName(),role, jwt);
        }

        //3. 不存在, 返回null
        return null;
    }

    @Override
    public LoginInfo stu_login(Student student) {
        //1. 调用mapper接口, 根据用户名和密码查询员工信息
        Student t = studentMapper.selectByUsernameAndPassword(student);

        //2. 判断: 判断是否存在这个教师, 如果存在, 组装登录成功信息
        if(t != null){
            log.info("登录成功, 学生信息: {}", t);
            //生成JWT令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("studentid", t.getStudentId());
            claims.put("username", t.getName());
            String jwt = JwtUtils.generateToken(claims);


            String role = "学生";

            return new LoginInfo(t.getStudentId(), t.getName(),role, jwt);
        }

        //3. 不存在, 返回null
        return null;
    }

}
